package com.yzt.message;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Keep;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.yzt.message.activity.GroupCreateActivity;
import com.yzt.message.custom.RXGiftMessageContent;
import com.yzt.message.custom.RXRedPkgMessageContent;
import com.yzt.message.custom.RedPackageTipMessageContent;
import com.yzt.zhuju.arouter.RouterMessage;
import com.yzt.zhuju.base.BaseFragment;
import com.yzt.zhuju.constants.IntentConstant;
import com.yzt.zhuju.dialog.BaseDialog;
import com.yzt.zhuju.dialog.DefaultDialog;
import com.yzt.zhuju.dialog.NewBaseBottomDialog;
import com.yzt.zhuju.event.RxBus;
import com.yzt.zhuju.event.im.RedPkgEvent;
import com.yzt.zhuju.event.im.SendGiftEvent;
import com.yzt.zhuju.event.im.SendRedPkgEvent;
import com.yzt.zhuju.event.message.MessageListAction;
import com.yzt.plugin_widget.tableBar.SlidingTabLayout;
import com.yzt.plugin_widget.viewPager.ViewPagerAdapter;
import com.yzt.message.fragment.MessageListSettingDialog;
import com.yzt.zhuju.network.entity.message.GiftInfoBean;
import com.yzt.zhuju.network.entity.message.RedBean;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.RongIM;
import io.rong.imlib.model.Conversation;

/**
 * 消息模块的父类列表
 */
@Keep
@Route(path = RouterMessage.FRAGMENT_MESSAGE)
public class MessageMainFragment extends BaseFragment {

    private SlidingTabLayout mTabLayout;

    private ViewPager mViewPager;

    private DefaultDialog fragment;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_message_father;
    }

    @Override
    public void installViews() {

        mTabLayout = findViewById(R.id.tablayout);
        mViewPager = findViewById(R.id.viewPager);

        //一键清除私信
        findViewById(R.id.iv_clear).setOnClickListener(v -> {
            MessageListSettingDialog settingDialogFragment =
                    (MessageListSettingDialog) NewBaseBottomDialog.showDialog(getActivity(),
                            MessageListSettingDialog.class);
            settingDialogFragment.setListener(type -> {
                switch (type) {
                    case MessageListSettingDialog.TYPE_SET_HAVE_READ:
                        showDialog(getString(R.string.ignore_unread_msg), getString(R.string.clear_pop_and_msg_not_lose), type);
                        break;
                    case MessageListSettingDialog.TYPE_DELETE_STRANGER_MESSAGE:
                        showDialog(getString(R.string.del_stranger_msg), getString(R.string.del_stranger_msg_desc), type);
                        break;
                    case MessageListSettingDialog.TYPE_DELETE_ALL_MESSAGE:
                        showDialog(getString(R.string.del_all_msg), getString(R.string.del_all_msg_desc), type);
                        break;
                    default:
                        break;
                }
                settingDialogFragment.dismiss();
            });
        });

        findViewById(R.id.img_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, GroupCreateActivity.class);
                startActivity(intent);
            }
        });

        addFragment();

        loadObserverRxBus();
    }

    /**
     * Event事件
     */
    private void loadObserverRxBus() {
        // 领取了红包发送消息
        observerRxBus(RedPkgEvent.class, event -> {
                    RedPackageTipMessageContent messageContent = new RedPackageTipMessageContent();
                    messageContent.setContent("「红包领取」");
                    RedBean messageExtra = new RedBean();
                    messageExtra.setSendTxt(event.getSendTxt());
                    messageExtra.setReceiveTxt(event.getReceiveTxt());
                    messageContent.setMessageExtra(messageExtra);
                    if (event.getTargetUid().length() > 10) {
                        Conversation.ConversationType conversationType = Conversation.ConversationType.GROUP;
                        io.rong.imlib.model.Message message = io.rong.imlib.model.Message.obtain(event.getTargetUid(), conversationType, messageContent);
                        RongIM.getInstance().sendMessage(message, null, null, null);
                    } else {
                        Conversation.ConversationType conversationType = Conversation.ConversationType.PRIVATE;
                        io.rong.imlib.model.Message message = io.rong.imlib.model.Message.obtain(event.getTargetUid(), conversationType, messageContent);
                        RongIM.getInstance().sendMessage(message, null, null, null);
                    }
                }
        );

        // 发送红包消息
        observerRxBus(SendRedPkgEvent.class, event -> {
                    RXRedPkgMessageContent messageContent = new RXRedPkgMessageContent();
                    messageContent.setContent("「红包」");
                    RedBean messageExtra = new RedBean();
                    messageExtra.setDes(event.getDsc());
                    messageExtra.setCandyCoin(event.getCandyCoin());
                    messageExtra.setRedPackageId(event.getRedPacketId());
                    messageContent.setMessageExtra(messageExtra);
                    Conversation.ConversationType conversationType = Conversation.ConversationType.PRIVATE;
                    io.rong.imlib.model.Message message = io.rong.imlib.model.Message.obtain(event.getTargetUid(), conversationType, messageContent);
                    RongIM.getInstance().sendMessage(message, null, null, null);
                }
        );

        // 发送礼物消息
        observerRxBus(SendGiftEvent.class, event -> {
                    if (event.getType() == 1) {
                        RXGiftMessageContent messageContent = new RXGiftMessageContent();
                        messageContent.setContent("「礼物」");
                        GiftInfoBean messageExtra = new GiftInfoBean();
                        messageExtra.setGiftIcon(event.getGiftIcon());
                        messageExtra.setCandyCoin(event.getCandyCoin());
                        messageExtra.setGiftName(event.getGiftName());
                        messageExtra.setGiftId(event.getGiftId());
                        messageContent.setMessageExtra(messageExtra);
                        Conversation.ConversationType conversationType = Conversation.ConversationType.PRIVATE;
                        io.rong.imlib.model.Message message = io.rong.imlib.model.Message.obtain(event.getTargetUid(), conversationType, messageContent);
                        RongIM.getInstance().sendMessage(message, null, null, null);
                    }
                }
        );
    }

    public void addFragment() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new MessageListFragment());
        List<String> titleList = new ArrayList<>();
        titleList.add("消息");
        mViewPager.setAdapter(new ViewPagerAdapter(getChildFragmentManager(), fragments, titleList));
        mViewPager.setOffscreenPageLimit(titleList.size());
        mTabLayout.setViewPager(mViewPager);
    }

    @Override
    public void netWorkRequest() {

    }

    private void showDialog(String title, String content, int type) {
        Bundle bundle = new Bundle();
        bundle.putString(IntentConstant.INTENT_CANCEL, getString(R.string.cancel));
        bundle.putString(IntentConstant.INTENT_SUBMIT, getString(R.string.sure));
        bundle.putString(IntentConstant.INTENT_CONTENT, content);
        bundle.putString(IntentConstant.INTENT_TITLE, title);
        fragment = (DefaultDialog) BaseDialog.showDialog(getActivity(), DefaultDialog.class, bundle);
        fragment.setListener(new DefaultDialog.OnSelectOnClickListener() {
            @Override
            public void selectSubmit() {
                RxBus.getDefault().post(new MessageListAction(type));
                fragment.dismiss();
            }

            @Override
            public void selectCancel() {
                fragment.dismiss();
            }
        });
    }
}